<?php
/**
 * Created by PhpStorm.
 * Script Name: BotGroupmember.php
 * Create: 2023/2/13 15:07
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;


use app\common\service\Queue;
use plugin\wechat\app\model\BotGroupmember as GroupMemberM;

class BotGroupmember extends Common
{

    /**
     * 新增群成员
     * @param array $params
     * @throws \think\db\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function addMember($params = []){
        if(!$gm = GroupMemberM::where([
            ['bot_id','=', $params['bot_id']],
            ['group_id' ,'=', $params['group_id']],
            ['wxid' ,'=', $params['wxid']]
        ])->find()){
            GroupMemberM::create($params);

            //记录进群统计数据
            Queue::push([
                'consumer' => ['\\plugin\\wechat\\app\\queue\\Bot', 'tjGroup'],
                'bot_id' => $params['bot_id'],
                'company_id' => $params['company_id'],
                'day' => date('Y-m-d'),
                'group_id' => $params['group_id'],
                'type' => 'add',
                'delay' => 10
            ]);
        }else{
            $params['id'] = $gm['id'];
            GroupMemberM::update($params);
        }
    }

    /**
     * 删除群成员
     * @param array $params
     * @throws \think\db\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function rmMember($params = []){
        GroupMemberM::where([
            ['bot_id' ,'=', $params['bot_id']],
            ['wxid' ,'=', $params['wxid']]
        ])->delete();

        //记录退群统计数据
        Queue::push([
            'consumer' => ['\\plugin\\wechat\\app\\queue\\Bot', 'tjGroup'],
            'bot_id' => $params['bot_id'],
            'company_id' => $params['company_id'],
            'day' => date('Y-m-d'),
            'group_id' => $params['group_id'],
            'type' => 'decr',
            'delay' => 10
        ]);
    }
}